/**
 * 全局类型
 */
if ('undefined' != typeof LBUtility) {
	
	throw	"core import deplicate";
}

var	LBUtility	= function () {

	/**
	 * null	: unregisted
	 * -1	: regiested
	 * 0	: loading
	 * 1	: loaded
	 */
	this._selectorStatMap	= {};
};

LBUtility._instance		= null;

LBUtility.getInstance	= function () {

	if (null == LBUtility._instance) {

		LBUtility._instance	= new LBUtility;
	}
	
	return	LBUtility._instance;
}

LBUtility.prototype.registSelector	= function (selector) {
	
	if ('undefined' == typeof this._selectorStatMap[selector]) {
	
		this._selectorStatMap[selector]	= -1;
	}
}

LBUtility.prototype.ready	= function (callback) {

	for (var attrSelector in this._selectorStatMap) {
		
		if (true !== this._selectorStatMap[attrSelector]) {
		
			this._readyCallback	= callback;
			
			return	;
		}
	}
	
	callback.call();
}

LBUtility.prototype.initailize	= function () {
	
	for (var selector in this._selectorStatMap) {
		
		if (-1 == this._selectorStatMap[selector]) {
			
			this._load(selector);
		}
	}
}

/**
 * 导入脚本文件
 */
LBUtility.prototype._load	= function (path) {
	
	this._selectorStatMap[path]	= 0;
	var	scriptElement			= document.createElement('script');
	scriptElement.type			= 'text\/javascript';
	var	host					= document.getElementsByTagName('head');
	
	if (!host) {
		
		host	= document.getElementsByTagName('body');
	}
	
	if (!host) {
		
		host	= document.getElementsByTagName('html');
	}
	
	scriptElement.addEventListener('load', function (event) {

		LBUtility.getInstance()._selectorStatMap[path]	= 1;

		if ('function' ==  typeof LBUtility.getInstance()._readyCallback) {
			
			for (var attrSelector in LBUtility.getInstance()._selectorStatMap) {
				
				if (1 !== LBUtility.getInstance()._selectorStatMap[attrSelector]) {
					
					return	;
				}
			}
			
			LBUtility.getInstance()._readyCallback.call();
		}
	});
	scriptElement.src	= path;
	host[0].appendChild(scriptElement);
}

/**
 * 调试对象
 */
LBUtility.prototype.debugObject	= function (object, DOMElement) {
	
	if ('object' != typeof DOMElement) {
		
		DOMElement	= document.createElement('pre');
		document.getElementsByTagName('body')[0].appendChild(DOMElement);
	}
	
	var	debugMessage	= '';
	
	for (var attr in object) {
		
		debugMessage	+= attr + ' = ' + object[attr] + "\n";
	}
	
	DOMElement.innerHTML	= debugMessage;
}
